﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
namespace Common.ADO
{
    public class DBManager
    {
        private static int GetDBType()
        {
            int DB = 0;
            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["ServerContentString"];
            if (settings != null)
            {
                string DBType = settings.ProviderName;
                if (DBType.ToString().ToLower().Equals("system.data.oracleclient"))
                {
                    DB = 1;
                }
                else if (DBType.ToString().ToLower().Equals("system.data.sqlclient"))
                {
                    DB = 2;
                }
                else if (DBType.ToString().ToLower().Equals("system.data.oledb"))
                {
                    DB = 5;
                }
            }
            else
            {
                string strDataClientType = System.Configuration.ConfigurationManager.AppSettings["Sql"];
                if (!string.IsNullOrEmpty(strDataClientType))
                {
                    DB = 3;
                }
                else
                {
                    string strDataClientType2 = System.Configuration.ConfigurationManager.AppSettings["Oracle"];
                    if (!string.IsNullOrEmpty(strDataClientType2))
                    {
                        DB = 4;
                    }
                    else
                    {
                        DB = 6;
                    }
                }

            }
            return DB;
        }

        /// <summary>
        /// 查询返回表里的所有记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<object> GetList(object obj)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().GetList(obj);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().GetList(obj);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().GetList(obj);
            }
            else if (GetDBType() == 6)
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().GetList(obj);
            }
            else
            {
                return null;
            }
            //else if (GetDBType() == 4)
            //{
            //    return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().GetList<T>();
            //}
            //else if (GetDBType() == 5)
            //{
            //    return Reflection.DAL.Managers.AccessManager.GetInstance().GetList<T>();
            //}
        }

        /// <summary>
        /// 分页返回表里的所有记录
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="PageSize">每页信息量</param>
        /// <param name="NowPage">当前页</param>
        /// <returns></returns>
        public static List<object> GetListByPage(int PageSize, int NowPage, out int PageCount, out int recordCount, object obj)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().GetListByPage(PageSize, NowPage, out PageCount, out recordCount, obj);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().GetListByPage(PageSize, NowPage, out PageCount, out recordCount, obj);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().GetListByPage(PageSize, NowPage, out PageCount, out recordCount, obj);
            }
            else if (GetDBType() == 6)
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().GetListByPage(PageSize, NowPage, out PageCount, out recordCount, obj);
            }
            else
            {
                PageCount = 0;
                recordCount = 0;
                return null;
            }
            //else if (GetDBType() == 4)
            //{
            //    return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().GetListByPage<T>(PageSize, NowPage, out PageCount, out recordCount);
            //}
            //else if (GetDBType() == 5)
            //{
            //    return Reflection.DAL.Managers.AccessManager.GetInstance().GetListByPage<T>(PageSize, NowPage, out PageCount, out recordCount);
            //}
        }

        /// <summary>
        /// 按条件分页返回表里的所有记录
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="PageSize">每页信息量</param>
        /// <param name="NowPage">当前页</param>
        /// <returns></returns>
        public static List<object> GetListByPageAndKeys(int PageSize, int NowPage, object obj, out int PageCount, out int recordCount)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().GetListByPageAndKeys(PageSize, NowPage, obj, out PageCount, out recordCount);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().GetListByPageAndKeys(PageSize, NowPage, obj, out PageCount, out recordCount);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().GetListByPageAndKeys(PageSize, NowPage, obj, out PageCount, out recordCount);
            }
            else if (GetDBType() == 6)
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().GetListByPageAndKeys(PageSize, NowPage, obj, out PageCount, out recordCount);
            }
            else
            {
                PageCount = 0;
                recordCount = 0;
                return null;
            }
            //else if (GetDBType() == 4)
            //{
            //    return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().GetListByPageAndKeys<T>(PageSize, NowPage, obj, out PageCount, out recordCount);
            //}
            //else if (GetDBType() == 5)
            //{
            //    return Reflection.DAL.Managers.AccessManager.GetInstance().GetListByPageAndKeys<T>(PageSize, NowPage, obj, out PageCount, out recordCount);
            //}
        }

        /// <summary>
        /// 查询按某条件查询的所有记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<object> GetAllByKey(string key, string value, object obj)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().GetAllByKey(key, value, obj);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().GetAllByKey(key, value, obj);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().GetAllByKey(key, value, obj);
            }
            else if (GetDBType() == 6)
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().GetAllByKey(key, value, obj);
            }
            else
            {
                return null;
            }

            //else if (GetDBType() == 4)
            //{
            //    return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().GetAllByKey<T>(key, value);
            //}
            //else if (GetDBType() == 5)
            //{
            //    return Reflection.DAL.Managers.AccessManager.GetInstance().GetAllByKey<T>(key, value);
            //}
        }

        /// <summary>
        /// 查询满足条件的记录是否唯一存在
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool IsExist(object obj)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().IsExist(obj);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().IsExist(obj);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().IsExist(obj);
            }
            else if (GetDBType() == 4)
            {
                return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().IsExist(obj);
            }
            else if (GetDBType() == 5)
            {
                return Reflection.DAL.Managers.AccessManager.GetInstance().IsExist(obj);
            }
            else
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().IsExist(obj);
            }
        }

        /// <summary>
        /// 查询所有满足某些条件的数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<object> GetSomeObject(object obj)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().GetSomeObject(obj);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().GetSomeObject(obj);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().GetSomeObject(obj);
            }
            else if (GetDBType() == 6)
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().GetSomeObject(obj);
            }
            else
            {
                return null;
            }
            //else if (GetDBType() == 4)
            //{
            //    return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().GetSomeObject<T>(obj);
            //}
            //else if (GetDBType() == 5)
            //{
            //    return Reflection.DAL.Managers.AccessManager.GetInstance().GetSomeObject<T>(obj);
            //}
        }


        /// <summary>
        /// 查询按某些条件查询的头几条记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<object> GetTopList(int count, object obj)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().GetTopList(count, obj);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().GetTopList(count, obj);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().GetTopList(count, obj);
            }
            else if (GetDBType() == 6)
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().GetTopList(count, obj);
            }
            else
            {
                return null;
            }
            //else if (GetDBType() == 4)
            //{
            //    return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().GetTopList<T>(count, obj);
            //}
            //else if (GetDBType() == 5)
            //{
            //    return Reflection.DAL.Managers.AccessManager.GetInstance().GetTopList<T>(count, obj);
            //}
        }

        /// <summary>
        /// 查询一条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static object GetModel(object obj)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().GetModel(obj);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().GetModel(obj);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().GetModel(obj);
            }
            else if (GetDBType() == 6)
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().GetModel(obj);
            }
            else
            {
                return null;
            }
            //else if (GetDBType() == 4)
            //{
            //    return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().GetModel<T>(id);
            //}
            //else if (GetDBType() == 5)
            //{
            //    return Reflection.DAL.Managers.AccessManager.GetInstance().GetModel<T>(id);
            //}
        }

        /// <summary>
        /// 保存oracle(ID无自动增长)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static int Insert(object obj)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().Insert(obj);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().Insert(obj);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().Insert(obj);
            }
            else if (GetDBType() == 4)
            {
                return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().Insert(obj);
            }
            else if (GetDBType() == 5)
            {
                return Reflection.DAL.Managers.AccessManager.GetInstance().Insert(obj);
            }
            else
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().Insert(obj);
            }
        }


        /// <summary>
        /// 编辑一个对象并保存
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="nameSpace"></param>
        public static void Edit(object obj)
        {
            if (GetDBType() == 1)
            {
                Reflection.DAL.Managers.OracleManager.GetInstance().Edit(obj);
            }
            else if (GetDBType() == 2)
            {
                Reflection.DAL.Managers.SQLManager.GetInstance().Edit(obj);
            }
            else if (GetDBType() == 3)
            {
                Reflection.DAL.Managers.WinFormOracleManager.GetInstance().Edit(obj);
            }
            else if (GetDBType() == 4)
            {
                Reflection.DAL.Managers.WinFormSQLManager.GetInstance().Edit(obj);
            }
            else if (GetDBType() == 5)
            {
                Reflection.DAL.Managers.AccessManager.GetInstance().Edit(obj);
            }
            else
            {
                Reflection.DAL.Managers.WinFormAccessManager.GetInstance().Edit(obj);
            }
        }
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="obj">对象</param>
        public static void Delete(object obj)
        {
            if (GetDBType() == 1)
            {
                Reflection.DAL.Managers.OracleManager.GetInstance().Delete(obj);
            }
            else if (GetDBType() == 2)
            {
                Reflection.DAL.Managers.SQLManager.GetInstance().Delete(obj);
            }
            else if (GetDBType() == 3)
            {
                Reflection.DAL.Managers.WinFormOracleManager.GetInstance().Delete(obj);
            }
            else if (GetDBType() == 4)
            {
                Reflection.DAL.Managers.WinFormSQLManager.GetInstance().Delete(obj);
            }
            else if (GetDBType() == 5)
            {
                Reflection.DAL.Managers.AccessManager.GetInstance().Delete(obj);
            }
            else
            {
                Reflection.DAL.Managers.WinFormAccessManager.GetInstance().Delete(obj);
            }
        }
        /// <summary>
        /// 删除表里的所有内容
        /// </summary>
        /// <param name="obj"></param>
        public static void DeleteAll(object obj)
        {
            if (GetDBType() == 1)
            {
                Reflection.DAL.Managers.OracleManager.GetInstance().DeleteAll(obj);
            }
            else if (GetDBType() == 2)
            {
                Reflection.DAL.Managers.SQLManager.GetInstance().DeleteAll(obj);
            }
            else if (GetDBType() == 3)
            {
                Reflection.DAL.Managers.WinFormOracleManager.GetInstance().DeleteAll(obj);
            }
            else if (GetDBType() == 4)
            {
                Reflection.DAL.Managers.WinFormSQLManager.GetInstance().DeleteAll(obj);
            }
            else if (GetDBType() == 5)
            {
                Reflection.DAL.Managers.AccessManager.GetInstance().DeleteAll(obj);
            }
            else
            {
                Reflection.DAL.Managers.WinFormAccessManager.GetInstance().DeleteAll(obj);
            }
        }

        /// <summary>
        /// 删除表里的几条数据（事务）
        /// </summary>
        /// <param name="obj"></param>
        public static void DeleteSome(object[] obj)
        {
            if (GetDBType() == 1)
            {
                Reflection.DAL.Managers.OracleManager.GetInstance().DeleteSome(obj);
            }
            else if (GetDBType() == 2)
            {
                Reflection.DAL.Managers.SQLManager.GetInstance().DeleteSome(obj);
            }
            else if (GetDBType() == 3)
            {
                Reflection.DAL.Managers.WinFormOracleManager.GetInstance().DeleteSome(obj);
            }
            else if (GetDBType() == 4)
            {
                Reflection.DAL.Managers.WinFormSQLManager.GetInstance().DeleteSome(obj);
            }
            else if (GetDBType() == 5)
            {
                Reflection.DAL.Managers.AccessManager.GetInstance().DeleteSome(obj);
            }
            else
            {
                Reflection.DAL.Managers.WinFormAccessManager.GetInstance().DeleteSome(obj);
            }
        }

        /// <summary>
        /// 按照数据的某个字段的值删除记录
        /// </summary>
        /// <param name="obj">对象</param>
        public static void DeleteColumByKey_Value(object obj)
        {
            if (GetDBType() == 1)
            {
                Reflection.DAL.Managers.OracleManager.GetInstance().DeleteColumByKey_Value(obj);
            }
            else if (GetDBType() == 2)
            {
                Reflection.DAL.Managers.SQLManager.GetInstance().DeleteColumByKey_Value(obj);
            }
            else if (GetDBType() == 3)
            {
                Reflection.DAL.Managers.WinFormOracleManager.GetInstance().DeleteColumByKey_Value(obj);
            }
            else if (GetDBType() == 4)
            {
                Reflection.DAL.Managers.WinFormSQLManager.GetInstance().DeleteColumByKey_Value(obj);
            }
            else if (GetDBType() == 5)
            {
                Reflection.DAL.Managers.AccessManager.GetInstance().DeleteColumByKey_Value(obj);
            }
            else
            {
                Reflection.DAL.Managers.WinFormAccessManager.GetInstance().DeleteColumByKey_Value(obj);
            }
        }

        public static List<object> GetListByPageLikeKey(int PageSize, int NowPage, object obj, out int PageCount, out int recordCount)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().GetListByPageLikeKey(PageSize, NowPage, obj, out PageCount, out  recordCount);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().GetListByPageLikeKey(PageSize, NowPage, obj, out PageCount, out  recordCount);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().GetListByPageLikeKey(PageSize, NowPage, obj, out PageCount, out  recordCount);
            }
            else if (GetDBType() == 6)
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().GetListByPageLikeKey(PageSize, NowPage, obj, out PageCount, out  recordCount);
            }
            else
            {
                PageCount = 0;
                recordCount = 0;
                return null;
            }
            //else if (GetDBType() == 4)
            //{
            //    return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().GetListByPageLikeKey<T>(PageSize, NowPage, obj, out PageCount, out  recordCount);
            //}
            //else if (GetDBType() == 5)
            //{
            //    return Reflection.DAL.Managers.AccessManager.GetInstance().GetListByPageLikeKey<T>(PageSize, NowPage, obj, out PageCount, out  recordCount);
            //}
        }
        public static bool IsJustExist(object obj)
        {
            if (GetDBType() == 1)
            {
                return Reflection.DAL.Managers.OracleManager.GetInstance().IsJustExist(obj);
            }
            else if (GetDBType() == 2)
            {
                return Reflection.DAL.Managers.SQLManager.GetInstance().IsJustExist(obj);
            }
            else if (GetDBType() == 3)
            {
                return Reflection.DAL.Managers.WinFormOracleManager.GetInstance().IsJustExist(obj);
            }
            else if (GetDBType() == 4)
            {
                return Reflection.DAL.Managers.WinFormSQLManager.GetInstance().IsJustExist(obj);
            }
            else if (GetDBType() == 5)
            {
                return Reflection.DAL.Managers.AccessManager.GetInstance().IsJustExist(obj);
            }
            else
            {
                return Reflection.DAL.Managers.WinFormAccessManager.GetInstance().IsJustExist(obj);
            }
        }
    }
}
